//
//  ServerHandle.m
//  购房通
//
//  Created by URANUS on 15/4/22.
//  Copyright (c) 2015年 URANUS. All rights reserved.
//

#import "ServerHandle.h"
#import "AppDelegate.h"
//#import "UserLoginViewController.h"
#define debug

@interface ServerHandle ()

@end

@implementation ServerHandle
/*
-(void)requestHandler:(NSString *)requestString{
    NSString *request = [ComAndDecomData compressData:requestString];
    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] init];
    [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
    manager.requestSerializer.timeoutInterval = 20.f;
    [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    manager.responseSerializer = [AFCompoundResponseSerializer serializer];
    NSDictionary *parameters = @{KEYNAME:request};
    [manager POST:SERVERACTION parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject){
        NSString *responseString = operation.responseString;
        responseString = [ComAndDecomData decompressData:responseString];
        //判断是否重复登录，有重复登录则跳转到登录界面，否则让返回数据由闭包处理
        NSDictionary *dic=[CommFunc dictionaryWithJsonString:responseString];
        NSString *state=[dic objectForKey:@"status"];
        if([state isEqualToString:@"relogin"]){
            NSString *deviceName=[dic objectForKey:@"deviceName"];
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"重复登录" message:[NSString stringWithFormat:@"登录的设备为：%@ ，如果不是您本人操作，请重新登录并更改密码",deviceName] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
            [self turnToLogin];
        }else if([state isEqualToString:@"inexist"]){
           [self turnToLogin];
        }else{
            if (_handleBlock != nil) {
                _handleBlock(responseString);
            }
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error){
#ifdef debug
        NSLog(@"---------------");
        NSLog(@"FAILURE : %@ --> %@", operation.responseString, error);
        NSLog(@"---------------");
#endif
        [self alertViewShow];
    }];
}
*/
//-(void)turnToLogin{
//
//    AppDelegate *delegate=[UIApplication sharedApplication].delegate;
//    UserLoginViewController *loginViewController=[[UserLoginViewController alloc] init];
//    delegate.window.rootViewController=loginViewController;
//    NSUserDefaults *userDefatluts = [NSUserDefaults standardUserDefaults];
//    NSDictionary *dictionary = [userDefatluts  dictionaryRepresentation];
//    for(NSString* key in [dictionary allKeys]){
//        [userDefatluts  removeObjectForKey:key];
//        [userDefatluts  synchronize];
//    }
//}
//
//-(void)requestHandler:(NSString *)requestString withHandleResposeBlock:(HandleResponseBlock) handleResponseBlock{
//    NSString *request = [ComAndDecomData compressData:requestString];
//    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] init];
//    [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
//    manager.requestSerializer.timeoutInterval = 20.f;
//    [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
//    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
//    manager.responseSerializer = [AFCompoundResponseSerializer serializer];
//    NSDictionary *parameters = @{KEYNAME:request};
//    [manager POST:SERVERACTION parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject){
//        NSString *responseString = operation.responseString;
//        responseString = [ComAndDecomData decompressData:responseString];
//        //判断是否重复登录，有重复登录则跳转到登录界面，否则让返回数据由闭包处理
//        NSDictionary *dic=[CommFunc dictionaryWithJsonString:responseString];
//        NSString *state=[dic objectForKey:@"status"];
//        if([state isEqualToString:@"relogin"]){
//            NSString *deviceName=[dic objectForKey:@"deviceName"];
//            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"重复登录" message:[NSString stringWithFormat:@"登录的设备为：%@ ，如果不是您本人操作，请重新登录并更改密码",deviceName] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//            [alert show];
//            [self turnToLogin];
//        }else if([state isEqualToString:@"inexist"]){
//            [self turnToLogin];
//        }else{
//            handleResponseBlock(responseString);
//        }
//        
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error){
//#ifdef debug
//        NSLog(@"---------------");
//        NSLog(@"FAILURE : %@ --> %@", operation.responseString, error);
//        NSLog(@"---------------");
//#endif
//        [self alertViewShow];
//    }];
//}
//
//-(void)requestHandler:(NSString *)requestString andUIView:(UIView *)view{
//    [UIFunc addUIViewIndicatorView:view];
//    NSString *request = [ComAndDecomData compressData:requestString];
//    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] init];
//    [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
//    manager.requestSerializer.timeoutInterval = 20.f;
//    [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
//    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
//    manager.responseSerializer = [AFCompoundResponseSerializer serializer];
//    NSDictionary *parameters = @{KEYNAME:request};
//    [manager POST:SERVERACTION parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject){
//        NSString *responseString = operation.responseString;
//        responseString = [ComAndDecomData decompressData:responseString];
//        //判断是否重复登录，有重复登录则跳转到登录界面，否则让返回数据由闭包处理
//        NSDictionary *dic=[CommFunc dictionaryWithJsonString:responseString];
//        NSString *state=[dic objectForKey:@"status"];
//        if([state isEqualToString:@"relogin"]){
//            NSString *deviceName=[dic objectForKey:@"deviceName"];
//            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"重复登录" message:[NSString stringWithFormat:@"登录的设备为：%@ ，如果不是您本人操作，请重新登录并更改密码",deviceName] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//            [alert show];
//            [self turnToLogin];
//        }else if([state isEqualToString:@"inexist"]){
//            [self turnToLogin];
//        }else{
//            if (_handleBlock != nil) {
//                _handleBlock(responseString);
//            }
//        }
//        
//        [UIFunc removeUIViewIndicatorView:view];
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error){
//#ifdef debug
//        NSLog(@"---------------");
//        NSLog(@"FAILURE : %@ --> %@", operation.responseString, error);
//        NSLog(@"---------------");
//#endif
//        [self alertViewShow];
//        [UIFunc removeUIViewIndicatorView:view];
//    }];
//}
//
//
//-(void)requestHandler:(NSString *)requestString andUIView:(UIView *)view withHandleResposeBlock:(HandleResponseBlock) handleResponseBlock{
//    [UIFunc addUIViewIndicatorView:view];
//    NSString *request = [ComAndDecomData compressData:requestString];
//    
//    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] init];
//    [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
//    manager.requestSerializer.timeoutInterval = 60.f;
//    [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
//    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
//    manager.responseSerializer = [AFCompoundResponseSerializer serializer];
//    NSDictionary *parameters = @{KEYNAME:request};
//    [manager POST:SERVERACTION parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject){
//        NSString *responseString = operation.responseString;
//        responseString = [ComAndDecomData decompressData:responseString];
//        NSLog(@"返回数据：%@",responseString);
//        //判断是否重复登录，有重复登录则跳转到登录界面，否则让返回数据由闭包处理
//        NSDictionary *dic=[CommFunc dictionaryWithJsonString:responseString];
//        NSString *state=[dic objectForKey:@"status"];
//        if([state isEqualToString:@"relogin"]){
//            NSString *deviceName=[dic objectForKey:@"deviceName"];
//            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"重复登录" message:[NSString stringWithFormat:@"登录的设备为：%@ ，如果不是您本人操作，请重新登录并更改密码",deviceName] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//            [alert show];
//            [self turnToLogin];
//        }else if([state isEqualToString:@"inexist"]){
//            [self turnToLogin];
//        }else{
//            handleResponseBlock(responseString);
//        }
//        [UIFunc removeUIViewIndicatorView:view];
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error){
//#ifdef debug
//        NSLog(@"---------------");
//        NSLog(@"FAILURE : %@ --> %@", operation.responseString, error);
//        NSLog(@"---------------");
//#endif
//        [self alertViewShow];
//        [UIFunc removeUIViewIndicatorView:view];
//    }];
//}
//
//- (void)alertViewShow{
//    alertView = [[UIAlertView alloc] initWithTitle:@"错误"
//                                           message:@"无法连接服务器，请检查网络连接"
//                                          delegate:nil
//                                 cancelButtonTitle:nil otherButtonTitles:nil, nil];
//    
//    [NSTimer scheduledTimerWithTimeInterval:3.0f
//                                     target:self
//                                   selector:@selector(dismissAlertView:)
//                                   userInfo:nil
//                                    repeats:NO];
//    
//    [alertView show];
//}
//
//- (void)dismissAlertView:(NSTimer*)timer {
//    [alertView dismissWithClickedButtonIndex:0 animated:YES];
//}

@end
